package com.zkadmin.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "sys_role")
public class SysRole extends AuditorAndVersionEntity {
	private static final long serialVersionUID = 848883491631970297L;
	@Column(unique = true, nullable = false, length = 30)
	private String role;
	@Column(unique = true, nullable = false, length = 50)
	private String name;

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "roles")
	private Set<SysUser> users = new HashSet<SysUser>();

	@ManyToMany(cascade = CascadeType.REFRESH)
	@JoinTable(name = "sys_role_resource", inverseJoinColumns = @JoinColumn(name = "resource_id"), joinColumns = @JoinColumn(name = "role_id"))
	private Set<SysResource> resources = new HashSet<SysResource>();

	public SysRole() {
	}

	public SysRole(String role, String name) {
		this.role = role;
		this.name = name;
	}

	public void addResource(SysResource resource) {
		if (resource != null) {
			this.resources.add(resource);
		}
	}

	public void removeResource(SysResource resource) {
		if (resource != null && resources.contains(resource)) {
			this.resources.remove(resource);
		}
	}

	@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	@JoinTable(name = "sys_roles_menus", inverseJoinColumns = @JoinColumn(name = "menu_id"), joinColumns = @JoinColumn(name = "role_id"))
	private Set<SysMenu> menus = new HashSet<SysMenu>();

	public void addMenu(SysMenu menu) {
		if (menu != null) {
			this.menus.add(menu);
		}
	}

	public void removeMenu(SysMenu menu) {
		if (menu != null && menus.contains(menu)) {
			this.menus.remove(menu);
		}
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<SysUser> getUsers() {
		return users;
	}

	public void setUsers(Set<SysUser> users) {
		this.users = users;
	}

	public Set<SysResource> getResources() {
		return resources;
	}

	public void setResources(Set<SysResource> resources) {
		this.resources = resources;
	}

	public Set<SysMenu> getMenus() {
		return menus;
	}

	public void setMenus(Set<SysMenu> menus) {
		this.menus = menus;
	}

}
